package com.kzd.gtgame.main.money

import android.content.Intent
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.LogUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.MallDetails
import com.kzd.gtgame.entity.UserAddress
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.KEY_ADDRESS_INFO
import com.kzd.gtgame.main.KEY_MONEY_MALL_DET
import com.kzd.gtgame.main.toMineAddressActivity
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_mall_sub.*

class MoneyMallSubActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMallDetails: MallDetails? by lazy {
        intent.extras?.getSerializable(KEY_MONEY_MALL_DET) as? MallDetails
    }

    private var mUserAddress: UserAddress? = null

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_mall_sub

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMoneyMallSubBar)
    }

    override fun initView() {

        llMoneyMallSubAddress.setOnClickAntiShakeListener {
            toMineAddressActivity(activity = this, requestCode = 100)
        }

        tvMoneyMallSubmit.setOnClickAntiShakeListener {
            val goodsId = mMallDetails?.goods_id
            val addressId = mUserAddress?.id
            when {
                goodsId == null -> toast("商品不能为空")
                addressId == null -> toast("请选择收货地址")
                else -> {
                    viewModel.moneyTaskMallSubmit(goodsId, addressId, num = 1)
                }
            }
        }
    }

    override fun initData() {
        mMallDetails?.let {
            ivMoneyMallSubGoodsIcon.loadImage(it.goods_thumb)
            tvMoneyMallSubGoodsName.text = it.goods_name
            tvMoneyMallSubGoodsPrice.text = StringBuilder()
                .append("消耗积分：").append(it.market_price).append("积分")
        }
    }

    override fun initViewMode() {
        viewModel.moneyTaskMallSubmitLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.message)
            finish()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 1) {
            val userAddress = (data?.getSerializableExtra(KEY_ADDRESS_INFO) as? UserAddress)
                .apply { mUserAddress = this }
            if (userAddress == null) {
                tvMoneyMallSubAddressSelect.visibility = View.VISIBLE
                clMoneyMallSubAddressContent.visibility = View.GONE
            } else {
                clMoneyMallSubAddressContent.visibility = View.VISIBLE
                tvMoneyMallSubAddressSelect.visibility = View.GONE
                tvMoneyMallSubAddressName.text = userAddress.consignee
                tvMoneyMallSubAddressPhone.text = userAddress.mobile
                tvMoneyMallSubAddressDet.text = userAddress.uaddress
            }
        }
    }


}